home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Freeware / First Page 2006 3.00 / fp2006-final-3.00-setup.exe / {app} / Iscripts / Cookie / display-users-name.izs < prev    next >
Text File  |  2005-09-28  |  7KB  |  179 lines

  1. <!NOWIZARD>
  2.  
  3. <!TITLE>Using cookies to remember and display a person's name
  4. <!/TITLE>
  5.  
  6. <!DESCRIPTION>This script will prompt the user to enter his/her name, and remember and display it whenever the user returns.
  7. <!/DESCRIPTION> 
  8.  
  9. <!CATEGORY>cookies<!/CATEGORY>
  10.  
  11. <!SCRIPT>
  12. <!-- START OF SCRIPT -->
  13. <script>
  14. <!--
  15. // Copyright (c) 1996-1997 Tomer Shiran. All rights reserved.
  16. // Permission given to use the script provided that this notice remains as is.
  17. // Additional scripts can be found at http://www.geocities.com/~yehuda/
  18.  
  19. // Boolean variable specified if alert should be displayed if cookie exceeds 4KB
  20. var caution = false
  21.  
  22. // name - name of the cookie
  23. // value - value of the cookie
  24. // [expires] - expiration date of the cookie (defaults to end of current session)
  25. // [path] - path for which the cookie is valid (defaults to path of calling document)
  26. // [domain] - domain for which the cookie is valid (defaults to domain of calling document)
  27. // [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
  28. // * an argument defaults when it is assigned null as a placeholder
  29. // * a null placeholder is not required for trailing omitted arguments
  30. function setCookie(name, value, expires, path, domain, secure) {
  31. var curCookie = name + "=" + escape(value) +
  32. ((expires) ? "; expires=" + expires.toGMTString() : "") +
  33. ((path) ? "; path=" + path : "") +
  34. ((domain) ? "; domain=" + domain : "") +
  35. ((secure) ? "; secure" : "")
  36. if (!caution || (name + "=" + escape(value)).length <= 4000)
  37. document.cookie = curCookie
  38. else
  39. if (confirm("Cookie exceeds 4KB and will be cut!"))
  40. document.cookie = curCookie
  41. }
  42.  
  43. // name - name of the desired cookie
  44. // * return string containing value of specified cookie or null if cookie does not exist
  45. function getCookie(name) {
  46. var prefix = name + "="
  47. var cookieStartIndex = document.cookie.indexOf(prefix)
  48. if (cookieStartIndex == -1)
  49. return null
  50. var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
  51. if (cookieEndIndex == -1)
  52. cookieEndIndex = document.cookie.length
  53. return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
  54. }
  55.  
  56. // name - name of the cookie
  57. // [path] - path of the cookie (must be same as path used to create cookie)
  58. // [domain] - domain of the cookie (must be same as domain used to create cookie)
  59. // * path and domain default if assigned null or omitted if no explicit argument proceeds
  60. function deleteCookie(name, path, domain) {
  61. if (getCookie(name)) {
  62. document.cookie = name + "=" + 
  63. ((path) ? "; path=" + path : "") +
  64. ((domain) ? "; domain=" + domain : "") +
  65. "; expires=Thu, 01-Jan-70 00:00:01 GMT"
  66. }
  67. }
  68.  
  69. // date - any instance of the Date object
  70. // * you should hand all instances of the Date object to this function for "repairs"
  71. // * this function is taken from Chapter 14, "Time and Date in JavaScript", in "Learn Advanced JavaScript Programming"
  72. function fixDate(date) {
  73. var base = new Date(0)
  74. var skew = base.getTime()
  75. if (skew > 0)
  76. date.setTime(date.getTime() - skew)
  77. }
  78.  
  79. var now = new Date()
  80. fixDate(now)
  81. now.setTime(now.getTime() + 31 * 24 * 60 * 60 * 1000)
  82. var name = getCookie("name")
  83. if (!name)
  84. name = prompt("Please enter your name:", "John Doe")
  85. setCookie("name", name, now)
  86. document.write("Hello " + name + "!")
  87. //-->
  88. </script>
  89.  
  90.  
  91. <!-- END OF SCRIPT -->
  92. <!/SCRIPT>
  93.  
  94. <!PREVIEW>
  95. <!-- START OF SCRIPT -->
  96. <script>
  97. <!--
  98. // Copyright (c) 1996-1997 Tomer Shiran. All rights reserved.
  99. // Permission given to use the script provided that this notice remains as is.
  100. // Additional scripts can be found at http://www.geocities.com/~yehuda/
  101.  
  102. // Boolean variable specified if alert should be displayed if cookie exceeds 4KB
  103. var caution = false
  104.  
  105. // name - name of the cookie
  106. // value - value of the cookie
  107. // [expires] - expiration date of the cookie (defaults to end of current session)
  108. // [path] - path for which the cookie is valid (defaults to path of calling document)
  109. // [domain] - domain for which the cookie is valid (defaults to domain of calling document)
  110. // [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
  111. // * an argument defaults when it is assigned null as a placeholder
  112. // * a null placeholder is not required for trailing omitted arguments
  113. function setCookie(name, value, expires, path, domain, secure) {
  114. var curCookie = name + "=" + escape(value) +
  115. ((expires) ? "; expires=" + expires.toGMTString() : "") +
  116. ((path) ? "; path=" + path : "") +
  117. ((domain) ? "; domain=" + domain : "") +
  118. ((secure) ? "; secure" : "")
  119. if (!caution || (name + "=" + escape(value)).length <= 4000)
  120. document.cookie = curCookie
  121. else
  122. if (confirm("Cookie exceeds 4KB and will be cut!"))
  123. document.cookie = curCookie
  124. }
  125.  
  126. // name - name of the desired cookie
  127. // * return string containing value of specified cookie or null if cookie does not exist
  128. function getCookie(name) {
  129. var prefix = name + "="
  130. var cookieStartIndex = document.cookie.indexOf(prefix)
  131. if (cookieStartIndex == -1)
  132. return null
  133. var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
  134. if (cookieEndIndex == -1)
  135. cookieEndIndex = document.cookie.length
  136. return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
  137. }
  138.  
  139. // name - name of the cookie
  140. // [path] - path of the cookie (must be same as path used to create cookie)
  141. // [domain] - domain of the cookie (must be same as domain used to create cookie)
  142. // * path and domain default if assigned null or omitted if no explicit argument proceeds
  143. function deleteCookie(name, path, domain) {
  144. if (getCookie(name)) {
  145. document.cookie = name + "=" + 
  146. ((path) ? "; path=" + path : "") +
  147. ((domain) ? "; domain=" + domain : "") +
  148. "; expires=Thu, 01-Jan-70 00:00:01 GMT"
  149. }
  150. }
  151.  
  152. // date - any instance of the Date object
  153. // * you should hand all instances of the Date object to this function for "repairs"
  154. // * this function is taken from Chapter 14, "Time and Date in JavaScript", in "Learn Advanced JavaScript Programming"
  155. function fixDate(date) {
  156. var base = new Date(0)
  157. var skew = base.getTime()
  158. if (skew > 0)
  159. date.setTime(date.getTime() - skew)
  160. }
  161.  
  162. var now = new Date()
  163. fixDate(now)
  164. now.setTime(now.getTime() + 31 * 24 * 60 * 60 * 1000)
  165. var name = getCookie("name")
  166. if (!name)
  167. name = prompt("Please enter your name:", "John Doe")
  168. setCookie("name", name, now)
  169. document.write("Hello " + name + "!")
  170. //-->
  171. </script>
  172.  
  173.  
  174.  
  175. <!-- END OF SCRIPT -->
  176. <!/PREVIEW>
  177.  
  178. <!RELATED>NONE<!/RELATED>
  179.